package com.t2c.tibetantochinese;

/**
 * Created by zoulu on 16/6/16.
 * application
 */

import android.app.Application;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import io.realm.DynamicRealm;
import io.realm.Realm;
import io.realm.RealmConfiguration;
import io.realm.RealmMigration;
import io.realm.exceptions.RealmMigrationNeededException;

/**
 * Created by zoulu on 16/5/16.
 * 程序入口
 */
public class MainApplication extends Application{
    private RequestQueue mRequestQueue;
    private static MainApplication mInstance;
    public static final String TAG = MainApplication.class.getSimpleName();

    public MainApplication() {
        mInstance = this;
    }

    /**
     * 获得application实例
     *
     */
    public static MainApplication getInstance() {
        if (mInstance == null) {
            mInstance = new MainApplication();
        }
        return mInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        //初始化realm
        Realm.init(mInstance);
    }

    public <T> void addToRequestQueue(Request<T> req, Object tag) {
        // set the default tag if tag is empty
        req.setTag(tag == null ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);

    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }


    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }


}

